package com.piece.file.controller;

import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.bootstrap.Valid;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.file.model.TbFileClient;
import com.piece.file.service.TbFileClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = {"文件客户端管理"})
@RequestMapping("/api/file/channel")
public class TbFileClientController extends FrameWorkController {

    @Resource
    private TbFileClientService tbFileClientService;

    @PostMapping("/list")
    @AclPermission(code = "file:channel:view")
    @ApiOperation(value = "查询文件客户端配置列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = tbFileClientService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/table")
    @AclPermission(code = "file:channel:view")
    @ApiOperation(value = "查询文件客户端配置分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = tbFileClientService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/load")
    @AclPermission(code = "file:channel:view")
    @ApiOperation(value = "加载文件客户端配置表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        String id = Convert.toStr(getRequest().getParameter("id"), null);
        TbFileClient tbFileClient = new TbFileClient();
        if (StringUtil.isNotEmpty(id)) {
            tbFileClient = tbFileClientService.findById(id);
        }
        return success(tbFileClient);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "file:channel:add,file:channel:edit")
    @ApiOperation(value = "保存文件客户端配置表单数据")
    @ApiImplicitParam(value = "文件客户端配置数据", dataTypeClass = TbFileClient.class, type = "body")
    public AjaxResponse save() {
        TbFileClient tbFileClient;
        try {
            String id = Convert.toStr(getRequest().getParameter("id"));
            if (null != id) {
                tbFileClient = tbFileClientService.findById(id);
            } else {
                tbFileClient = new TbFileClient();
            }
            initDB(tbFileClient);
            tbFileClientService.insertOrUpdate(tbFileClient);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete/{id}")
    @AclPermission(code = "file:channel:delete")
    @ApiOperation(value = "删除文件客户端配置数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        tbFileClientService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "file:channel:delete")
    @ApiOperation(value = "删除文件客户端配置数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            tbFileClientService.delete(Convert.toStrArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @GetMapping("/checkUnique")
    @ApiOperation(value = "检查客户类别是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "clientId"),
            @ApiImplicitParam(name = "clientType", value = "客户类别")
    })
    public Valid checkUnique() {
        String clientId = getRequest().getParameter("clientId");
        String clientType = getRequest().getParameter("clientType");
        Valid valid = new Valid();
        boolean result = tbFileClientService.checkUnique(clientId, clientType);
        valid.setValid(result);
        return valid;
    }
}
